package org.pjlib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.recyclerview.widget.RecyclerView;

import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.SizeUtils;
import org.pjlib.Logs;
import org.pjlib.R;


/**
 * Author: ws.
 * Date: 2018/2/1.
 * Description: //TODO 表格的控件
 */

public class ChequerView extends ViewGroup {
    private static final String TAG = ChequerView.class.getSimpleName();
    private RecyclerView.Adapter adapter;
    private int mViewWidth, mViewHeight;
    private int spacWidth, spacHeight;
    private int column;
    private boolean isChequer;
    private OnItemClickListener onItemClickListener;
    private int dp1;
    public ChequerView(Context context) {
        this(context, null);
    }

    public ChequerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChequerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        int spac = SizeUtils.dp2px(  5f);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ChequerView);
        spacWidth = (int) a.getDimension(R.styleable.ChequerView_spacWidth, spac);
        spacHeight = (int) a.getDimension(R.styleable.ChequerView_spacHeight, spac);
        column = a.getInt(R.styleable.ChequerView_column, 4);
        isChequer=a.getBoolean(R.styleable.ChequerView_is_chequer,true);
        a.recycle();
        dp1=SizeUtils.dp2px( 1);
    }

    public void setChequer(boolean chequer) {
        isChequer = chequer;
    }

    public int getSpacWidth() {
        return spacWidth;
    }

    public void setSpacWidth(int spacWidth) {
        this.spacWidth = spacWidth;
    }

    public int getSpacHeight() {
        return spacHeight;
    }

    public void setSpacHeight(int spacHeight) {
        this.spacHeight = spacHeight;
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
        invalidate();
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public RecyclerView.Adapter getAdapter() {
        return adapter;
    }

    public void setAdapter(RecyclerView.Adapter adapter) {
        this.adapter = adapter;
        setView(adapter);
    }

    private void setView(RecyclerView.Adapter adapter) {
        addViewChild();
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                ChequerView.this.removeAllViews();
                addViewChild();
                requestLayout();
            }
        });
    }

    @Override
    public void addView(View child) {
        super.addView(child);
        requestLayout();
    }

    private void addViewChild() {
        if (adapter == null) {
            return;
        }
        Logs.i(this, "in:" + adapter.getItemCount());
        for (int i = 0; i < adapter.getItemCount(); i++) {
            final RecyclerView.ViewHolder holder = adapter.createViewHolder(this, adapter.getItemViewType(i));
            final int index = i;
            holder.itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(ChequerView.this, holder.itemView, index);
                    }
                }
            });
            this.addView(holder.itemView);
            adapter.bindViewHolder(holder, i);
        }
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            mViewWidth = widthSize;
        } else {
            mViewWidth = LayoutParams.MATCH_PARENT;
        }
        if (heightMode == MeasureSpec.EXACTLY) {//
            mViewHeight = heightSize;
        }
        final int cCount = getChildCount();
        int row = getRow(cCount);
        for (int i = 0; i < cCount; i++) {
            View child = getChildAt(i);
            measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
            if(heightMode != MeasureSpec.EXACTLY){
                if (!isChequer && child.getMeasuredHeight()> 0) {
                    mViewHeight = (row - 1) * spacHeight + (row * child.getMeasuredHeight());
                } else {
                    int childW = (mViewWidth - (column - 1) * spacWidth) / column;
                    mViewHeight = childW * row + (row - 1) * spacHeight;
                }
            }
            setChildWH(child);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
        }
        setMeasuredDimension(mViewWidth, mViewHeight);
    }

    private void setChildWH(View child) {
        int childW = (mViewWidth - (column - 1) * spacWidth) / column;
        int row = getRow(getChildCount());
        int childH = (mViewHeight - (row - 1) * spacHeight) / row;
        LayoutParams params = child.getLayoutParams();
        params.width = childW;
        if (params.height < 0) {
            params.height = childH;
        }
        //  params.height=childH;
    }

    private int getRow(int count) {
        int s = count % column > 0 ? 1 : 0;
        int row = count / column + s;
        return row;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            int childWidth = child.getLayoutParams().width;
            int childHeight = child.getLayoutParams().height;
            int childColumn = i % column;//第几列
            int childRow = getRow(i + 1);
            int shiftX = childColumn * spacWidth;
            int shiftY = (childRow - 1) * spacHeight;
            if (childColumn + 1 == column) {
                child.layout(childColumn * childWidth + shiftX, (childRow - 1) * childHeight + shiftY, (childColumn + 1) * childWidth + shiftX + spacWidth, childRow * childHeight + shiftY);
            } else
                child.layout(childColumn * childWidth + shiftX, (childRow - 1) * childHeight + shiftY, (childColumn + 1) * childWidth + shiftX, childRow * childHeight + shiftY);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(ChequerView chequerView, View item, int postion);
    }
}
